package com.kmzx.one.project.config.web;

import com.kmzx.one.project.net.BaseResult;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Configuration
class GateWayErrorController implements ErrorController {

    @Override
    public String getErrorPath() {
        return "/error";
    }


    @RequestMapping("/error")
    public BaseResult handleError(HttpServletRequest request) {
        BaseResult result = BaseResult.Builder.FAIL();

        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == 401) {
            result.initErrCodeAndMsg(401,"路径权限非法");
            result.setData(null);
            return result;
        } else if (statusCode == 404) {
            result.initErrCodeAndMsg(404,"路径不存在");
            result.setData(null);
            return result;
        } else if (statusCode == 403) {
            result.initErrCodeAndMsg(403,"路径不允许");
            result.setData(null);
            return result;
        } else {
            result.initErrorCode(500);
            result.initErrorMsg("服务器正忙!");
            result.setData(null);
            return result;
        }

    }
}
